/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipartek.migracion.factoria;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.ipartek.migracion.dao.MySqlPersonaDAO;
import com.ipartek.migracion.interfaces.InterfacePersonaDAO;

/**
 * 
 * @author Curso
 */
public class MySqlDAOFactory extends DAOFactory {

	// Conexion

	public static final String DRIVER_MYSQL = "com.mysql.jdbc.Driver";
	public static final String DB_PORT = "3306";
	public static String db_name;
	public static String db_user;
	public static String db_pass;
	public static String db_server;
	public static String db_url;

	public final String TABLA_ALUMNOS = "alumno";
	public final String DB_CAMPO_ORDENACION = "num_tiradas";

	private static Logger log = Logger.getLogger(MySqlDAOFactory.class);;

	// Constructor
	// ************************************************************************
	public MySqlDAOFactory() {
		Properties prop = new Properties();
		// StringBuffer sql = null;
		String rutaConfig = "config/configuracion.properties";
		PropertyConfigurator.configure("config/log4j.properties");

		try {
			FileInputStream fis = new FileInputStream(rutaConfig);
			prop.load(fis);

			db_name = prop.getProperty("database");
			db_user = prop.getProperty("dbuser");
			db_pass = prop.getProperty("dbpass");
			db_server = prop.getProperty("dbserver");

		} catch (FileNotFoundException e) {
			log.error("FileNotFoundException " + e.getMessage());
		} catch (IOException e) {
			log.error("IOException " + e.getMessage());
		} catch (Exception e) {
			log.error("Exception " + e.getMessage());
		}
	}

	// Metodos
	// ************************************************************************

	// CONECTAR Y DESCONECTAR

	// Conectar con la base de datos
	public static Connection conectar() throws Exception {
		Connection conexion = null; // Creamos un tipo connection
		try {

			Class.forName(DRIVER_MYSQL);
			db_url = "jdbc:mysql://" + db_server + ":" + DB_PORT + "/"
					+ db_name;
			conexion = DriverManager.getConnection(db_url, db_user, db_pass);
			conexion.setAutoCommit(false);

		} catch (Exception e) {
			log.error("Exception " + e.getMessage());
		}
		if (conexion == null) {
			throw new Exception("Problema al conectar BBDD conexion="
					+ conexion);
		}
		return conexion;
	}

	// Desconectar con la base de datos
	public static void desconectar(Connection conexion) {
		if (conexion != null) {
			try {
				conexion.close();
			} catch (SQLException e) {
				while (e != null) {
					log.error("Message: " + e.getMessage() + "SQLState: "
							+ e.getSQLState() + "ErrorCode: "
							+ e.getErrorCode());
					e = e.getNextException();
				}
			}
		}
	}

	// Metodos que hay que implementar al extender de DAOFActory
	// *************************************************************************
	@Override
	public InterfacePersonaDAO getInterfacePersonaDAO() {
		return new MySqlPersonaDAO();
	}
}
